/**
 * Created by Baggio on 2019-1-14.
 */
$(function () {

    $("#jl").on("change", function () {
        console.log("11");
        if ($(this).prop("checked")) {
            qx();
        } else {
            qxqx();
        }
    })
    $(".pt1").on("change", function () {
        if ($(".pt1").length == $(".pt1:checked").length) {
            $("#jl").prop("checked", true);
        } else {
            $("#jl").prop("checked", false);

        }
    })
    function qx() {
        for (var i = 0; i < $(".pt1").length; i++) {
            $(".pt1").eq(i).prop("checked", true);
        }
    }

    function qxqx() {
        for (var i = 0; i < $(".pt1").length; i++) {
            $(".pt1").eq(i).prop("checked", false);
        }
    }
function getCheckedShopCarIds() {
    var shopCarIds = "";

    $('input[name="pt1"]:checked').each(function(){
        shopCarIds+=$(this).val()+",";
    });
    return shopCarIds.substr(0,shopCarIds.length-1);
}
    $("#del").click(function () {
        var shopCarIds =getCheckedShopCarIds();

        var postData={shoppingCartIds:shopCarIds};
        $.ajax({
            type: "post",
            url: "/shoppingCart/deleteShoppingCart",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(res);
                if (res.code != 1) {
                    alert(res.msg);
                    return;
                }
                console.log(res.data);

                alert("删除成功！");
            }

        });

    });
    $("#toPay").click(function () {

        var shopCarIds =getCheckedShopCarIds();

        var postData={shoppingCartIds:shopCarIds};
        $.ajax({
            type: "post",
            url: "/shoppingCart/payShoppingCart",
            dataType: "json",
            async: false,
            data: postData,
            success: function (res) {
                console.log(res);
                if (res.code != 1) {
                    alert(res.msg);
                    return;
                }
                var orderId = res.data.orderId;
                if (orderId == null || orderId == undefined || orderId == '') {
                    alert("订单生成失败！");
                }
                window.location.href = "/pay/" + orderId;
            }

        });
    });
})